package com.jeesite.modules.dataScreen.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeesite.modules.dataScreen.entity.SlCbbf;
import com.jeesite.modules.dataScreen.entity.SlCbjy;
import com.jeesite.modules.dataScreen.utils.ComputeUtil;
import com.jeesite.modules.dataScreen.web.vo.NameValueVo;
import com.jeesite.modules.dataScreen.web.vo.Result;
import com.jeesite.modules.dataScreen.web.vo.ShuiluVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author: zhaosenlin
 * @date: 2024/5/14 14:08
 */
@RestController
@Api(tags = "水路接口")
@RequestMapping("/shuilu")
@Slf4j
public class ShuiluApi extends BaseController {

    @ApiOperation("水路-船舶")
    @GetMapping("cb")
    public Result<ShuiluVo.A> cb() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "水路-船舶");
        ShuiluVo.A a = new ShuiluVo.A();
        fillDataByTiaoBao(allData(), ShuiluVo.A.class, a);
        Page<SlCbbf> slCbbfPage = themeDbDao.slCbbf(Page.of(1, 1));
        a.setValue1(slCbbfPage.getTotal());

        a.setValue2(NumberUtil.add(a.getValue78(), a.getValue79()).intValue());

        // Integer liangjiangyouCbqys = sjtbDbDao.liangjiangyouCbqys("1");
        // Integer sanxiaCbqys        = sjtbDbDao.sanxiaCbqys("1");
        // a.setValue8(ZERO);
        // a.setValue9(nullToZero(liangjiangyouCbqys));
        // a.setValue10(nullToZero(sanxiaCbqys));
        // a.setValue7(a.getValue8() + a.getValue9() + a.getValue10());
        // a.setValue8(getTianbaoValue("运输船舶总数-其他船舶总数").map(Integer::valueOf).orElse(0));
        // a.setValue9(getTianbaoValue("运输船舶总数-两江游船舶总数").map(Integer::valueOf).orElse(0));
        // a.setValue10(getTianbaoValue("运输船舶总数-三峡游船舶总数").map(Integer::valueOf).orElse(0));
        a.setValue7(NumberUtil.add(a.getValue8(), a.getValue9(), a.getValue10()).intValue());
        a.setValue6(String.valueOf(NumberUtil.add(a.getValue7(), a.getValue11()).intValue()));

        return Result.OK(a).apiInfo(fieldMean);
    }

    @ApiOperation("水路-港口码头")
    @GetMapping("gkmt")
    public Result<ShuiluVo.B> gkmt() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "水路-港口码头");
        ShuiluVo.B b = new ShuiluVo.B();
        fillDataByTiaoBao(allData(), ShuiluVo.B.class, b);
        List<Map<String, String>> gkmtData = themeDbDao.getByZbfl("港口码头泊位通过能力");
        if (!CollectionUtils.isEmpty(gkmtData)) {
            String zbmc;
            String sjzWb;
            String result = null;
            for (Map<String, String> map : gkmtData) {
                zbmc  = map.get("zbmc");
                sjzWb = map.get("sjz_wb");
                if (StringUtils.isNotBlank(sjzWb)) {
                    result = map.get("result");
                    if ("港口码头泊位通过能力-货运".equals(zbmc)) {
                        b.setValue25(result);
                    } else if ("港口码头泊位通过能力-货运-集装箱".equals(zbmc)) {
                        b.setValue26(result);
                    } else if ("港口码头泊位通过能力-货运-干散件杂".equals(zbmc)) {
                        b.setValue28(result);
                    } else if ("港口码头泊位通过能力-货运-滚装汽车".equals(zbmc)) {
                        b.setValue27(result);
                    }
                }
            }
        }
        return Result.OK(b).apiInfo(fieldMean);
    }

    @ApiOperation("水路-航道")
    @GetMapping("hd")
    public Result<ShuiluVo.C> hd() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "水路-航道");
        ShuiluVo.C c = new ShuiluVo.C();
        fillDataByTiaoBao(allData(), ShuiluVo.C.class, c);

        List<NameValueVo> nameValueVoList = new ArrayList<>();
        nameValueVoList.add(new NameValueVo().setName("一级").setValue(getTianbaoValue("内河航道里程-一级").orElse("0")));
        nameValueVoList.add(new NameValueVo().setName("二级").setValue(getTianbaoValue("内河航道里程-二级").orElse("0")));
        nameValueVoList.add(new NameValueVo().setName("三级").setValue(getTianbaoValue("内河航道里程-三级").orElse("0")));
        nameValueVoList.add(new NameValueVo().setName("四级").setValue(getTianbaoValue("内河航道里程-四级").orElse("0")));
        nameValueVoList.add(new NameValueVo().setName("五级").setValue(getTianbaoValue("内河航道里程-五级").orElse("0")));
        nameValueVoList.add(new NameValueVo().setName("六级").setValue(getTianbaoValue("内河航道里程-六级").orElse("0")));
        nameValueVoList.add(new NameValueVo().setName("七级").setValue(getTianbaoValue("内河航道里程-七级").orElse("0")));
        nameValueVoList.add(new NameValueVo().setName("等外级").setValue(getTianbaoValue("内河航道里程-等外级").orElse("0")));
        c.setNameValues1(nameValueVoList);

        c.setValue42("99");

        return Result.OK(c).apiInfo(fieldMean);
    }

    @ApiOperation("水路-水路运输")
    @GetMapping("slys")
    public Result<ShuiluVo.D> slys(@RequestParam(required = false, defaultValue = "1") String tjwdfl) {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "水路-水路运输");

        ShuiluVo.D d = new ShuiluVo.D();
        fillDataByTiaoBao(allData(), ShuiluVo.D.class, d);

        // Integer liangjiangyouKyl = sjtbDbDao.liangjiangyouKyl("1");
        // Integer sanxiaKyl        = sjtbDbDao.sanxiaKyl("1");
        // d.setValue54(nullToZero(sanxiaKyl));
        // d.setValue55(nullToZero(liangjiangyouKyl));
        // d.setValue56(ZERO);
        d.setValue53(NumberUtil.add(d.getValue54(), d.getValue55(), d.getValue56()).doubleValue());

        // Integer liangjiangyouCbqys = sjtbDbDao.liangjiangyouCbqys("2");
        // Integer sanxiaCbqys        = sjtbDbDao.sanxiaCbqys("2");
        // Integer qitaCbqys          = themeDbDao.qitaCbqys("2");
        // Integer kehuoyunCbqys      = themeDbDao.kehuoyunCbqys("2");
        // d.setValue59(nullToZero(liangjiangyouCbqys));
        // d.setValue60(nullToZero(sanxiaCbqys));
        // d.setValue66(nullToZero(qitaCbqys));
        // d.setValue67(nullToZero(kehuoyunCbqys));
        d.setValue58(NumberUtil.add(d.getValue59(), d.getValue60(), d.getValue66(), d.getValue67()).intValue());

        // d.setValue52(NumberUtil.add(d.getValue116(), d.getValue117(), d.getValue118(), d.getValue119()).toString());

        Integer liangjiangyouKyl = sjtbDbDao.liangjiangyouKyl("1");
        Integer sanxiaKyl = sjtbDbDao.sanxiaKyl("1");
        d.setValue108(ComputeUtil.divToStr(NumberUtil.toStr(liangjiangyouKyl), "18000"));
        d.setValue109(ComputeUtil.divToStr(NumberUtil.toStr(sanxiaKyl), "18000"));

        return Result.OK(d).apiInfo(fieldMean);
    }

    @ApiOperation("水路-交通安全")
    @GetMapping("jtaq")
    public Result<ShuiluVo.E> jtaq() {
        Map<String, Object> fieldMean = new LinkedHashMap<>();
        fieldMean.put("title", "水路-交通安全");
        ShuiluVo.E e = new ShuiluVo.E();
        fillDataByTiaoBao(allData(), ShuiluVo.E.class, e);

        fillFieldInfo(e, fieldMean);
        return Result.OK(e).apiInfo(fieldMean);
    }

    @ApiOperation("船舶报废")
    @GetMapping("cbbf")
    public Result<Page<SlCbbf>> cbbf(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<SlCbbf> chuanbobaofei = themeDbDao.slCbbf(Page.of(page, limit));
        return Result.OK(chuanbobaofei);
    }

    @ApiOperation("船舶检验")
    @GetMapping("cbjy")
    public Result<Page<SlCbjy>> cbjy(
            @ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam("数据条数") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        Page<SlCbjy> chuanbobaofei = themeDbDao.slCbjy(Page.of(page, limit));
        return Result.OK(chuanbobaofei);
    }

    @ApiOperation("三峡游当日实时客运量")
    @GetMapping("sanxiakyl")
    public Result<List<NameValueVo>> sanxiakyl(@ApiParam("条数限制") @RequestParam(required = false, defaultValue = "7") Integer limit) {
        String   day = DateUtil.format(DateUtil.date(), "yyyy-MM-dd 00");

        List<NameValueVo>        data            = sjtbDbDao.sanxiaKylDays(day, limit);
        Map<String, NameValueVo> map             = CollUtil.toMap(data, new HashMap<>(), NameValueVo::getName);
        List<NameValueVo>        nameValueVoList = new ArrayList<>();

        for (DateTime t : new DateRange(DateUtil.date().setField(DateField.HOUR_OF_DAY, 9),
                DateUtil.date().setField(DateField.HOUR_OF_DAY, 24), DateField.HOUR_OF_DAY)) {
            String tStr = DateUtil.format(t, "yyyy-MM-dd HH");
            // 获取小时对应的值，为null用默认值
            // NameValueVo nameValueVo = map.getOrDefault(tStr, new NameValueVo().setName(tStr).setValue("0"));
            NameValueVo nameValueVo = map.get(tStr);
            if (nameValueVo == null) {
                continue;
            }
            nameValueVoList.add(nameValueVo.setName(nameValueVo.getName() + ":00"));
        }
        nameValueVoList.sort(Comparator.comparing(NameValueVo::getName));

        // ArrayList<NameValueVo> data = new ArrayList<>();
        // data.add(new NameValueVo().setName("1").setValue("1"));
        // data.add(new NameValueVo().setName("2").setValue("2"));
        // data.add(new NameValueVo().setName("3").setValue("3"));
        return Result.OK(nameValueVoList);
    }

    @ApiOperation("两江游当日实时客运量")
    @GetMapping("liangjiangyoukyl")
    public Result<List<NameValueVo>> liangjiangyoukyl(@ApiParam("条数限制") @RequestParam(required = false, defaultValue = "7") Integer limit) {
        String   day = DateUtil.format(DateUtil.date(), "yyyy-MM-dd 00");

        List<NameValueVo>        data            = sjtbDbDao.liangjiangyouKylDays(day, limit);
        Map<String, NameValueVo> map             = CollUtil.toMap(data, new HashMap<>(), NameValueVo::getName);
        List<NameValueVo>        nameValueVoList = new ArrayList<>();

        for (DateTime t : new DateRange(DateUtil.date().setField(DateField.HOUR_OF_DAY, 9),
                DateUtil.date().setField(DateField.HOUR_OF_DAY, 24), DateField.HOUR_OF_DAY)) {
            String tStr = DateUtil.format(t, "yyyy-MM-dd HH");
            // 获取小时对应的值，为null用默认值
            // NameValueVo nameValueVo = map.getOrDefault(tStr, new NameValueVo().setName(tStr).setValue("0"));
            NameValueVo nameValueVo = map.get(tStr);
            if (nameValueVo == null) {
                continue;
            }
            nameValueVoList.add(nameValueVo.setName(nameValueVo.getName() + ":00"));
        }
        nameValueVoList.sort(Comparator.comparing(NameValueVo::getName));

        // ArrayList<NameValueVo> data = new ArrayList<>();
        // data.add(new NameValueVo().setName("1").setValue("1"));
        // data.add(new NameValueVo().setName("2").setValue("2"));
        // data.add(new NameValueVo().setName("3").setValue("3"));
        return Result.OK(nameValueVoList);
    }

    @ApiOperation("水路-运输(分时段)")
    @GetMapping("ys")
    public Result<ShuiluVo.A> ys(
            @ApiParam("季度，数字 1表示一季度，依次类推") @RequestParam(required = false) Integer region) {
        if (region == null) {
            region = DateUtil.quarter(DateUtil.date());
        }
        ShuiluVo.A a = new ShuiluVo.A();
        fillDataByTiaoBao(allData(), ShuiluVo.A.class, a, jd(region));
        return Result.OK(a);
    }

    @ApiOperation("水路-资源(分时段)")
    @GetMapping("slzy")
    public Result<ShuiluVo.A> slzy(
            @ApiParam("季度，数字 1表示一季度，依次类推") @RequestParam(required = false) Integer region) {
        if (region == null) {
            region = DateUtil.quarter(DateUtil.date());
        }
        ShuiluVo.A a = new ShuiluVo.A();
        fillDataByTiaoBao(allData(), ShuiluVo.A.class, a, jd(region));
        return Result.OK(a);
    }

    @ApiOperation("水路-在建航道整治工程项目 在建航运 (电) 枢纽项目(分时段)")
    @GetMapping("slzjhdzzgcxm")
    public Result<ShuiluVo.C> zjhdzzgcxm(
            @ApiParam("月份，数字 1表示一月份，依次类推") @RequestParam(required = false) Integer region) {
        if (region == null) {
            region = DateUtil.month(DateUtil.date()) + 1;
        }
        ShuiluVo.C c = new ShuiluVo.C();
        fillDataByTiaoBao(allData(), ShuiluVo.C.class, c, yf(region));
        return Result.OK(c);
    }

    @ApiOperation("水路-水路客运(分时段)")
    @GetMapping("slky")
    public Result<ShuiluVo.D> slky(
            @ApiParam("月份，数字 1表示一月份，依次类推") @RequestParam(required = false) Integer region) {
        if (region == null) {
            region = DateUtil.month(DateUtil.date());
        }
        ShuiluVo.D d = new ShuiluVo.D();
        fillDataByTiaoBao(allData(), ShuiluVo.D.class, d, yf(region));
        d.setValue53(NumberUtil.add(d.getValue54(), d.getValue55(), d.getValue56()).doubleValue());
        // d.setValue52(NumberUtil.add(d.getValue116(), d.getValue117(), d.getValue118(), d.getValue119()).toString());

        Integer liangjiangyouKyl = sjtbDbDao.liangjiangyouKyl("1");
        Integer sanxiaKyl = sjtbDbDao.sanxiaKyl("1");
        d.setValue108(ComputeUtil.divToStr(NumberUtil.toStr(liangjiangyouKyl), "18000"));
        d.setValue109(ComputeUtil.divToStr(NumberUtil.toStr(sanxiaKyl), "18000"));
        return Result.OK(d);
    }

    @ApiOperation("水路-水路货运运行信息(分时段)")
    @GetMapping("slhyyxxx")
    public Result<ShuiluVo.D> slhyyxxx(
            @ApiParam("季度，数字 1表示一季度，依次类推") @RequestParam(required = false) Integer region) {
        if (region == null) {
            region = DateUtil.quarter(DateUtil.date());
        }
        ShuiluVo.D d = new ShuiluVo.D();
        fillDataByTiaoBao(allData(), ShuiluVo.D.class, d, jd(region));
        d.setValue53(NumberUtil.add(d.getValue54(), d.getValue55(), d.getValue56()).doubleValue());
        // d.setValue52(NumberUtil.add(d.getValue116(), d.getValue117(), d.getValue118(), d.getValue119()).toString());

        Integer liangjiangyouKyl = sjtbDbDao.liangjiangyouKyl("1");
        Integer sanxiaKyl = sjtbDbDao.sanxiaKyl("1");
        d.setValue108(ComputeUtil.divToStr(NumberUtil.toStr(liangjiangyouKyl), "18000"));
        d.setValue109(ComputeUtil.divToStr(NumberUtil.toStr(sanxiaKyl), "18000"));
        return Result.OK(d);
    }
}
